<?php

function getLadder($leagueId) {
	$fixture = getCurrentFixture($leagueId);
	$count = count($fixture);
	$currentRound = getCurrentRound();
	
	$teamResults = array();
	for ($i = 0; $i < $count; $i++) {
		if ($fixture[$i]->homePlayerScores == null || $fixture[$i]->round >= $currentRound) {
			break;
		}
		
		$homeTeamResult = findTeamResult($fixture[$i]->homeTeamId, $teamResults);
		if ($homeTeamResult == null) {
			$homeTeamResult = new TeamResult(getTeamById($fixture[$i]->homeTeamId, false));
			$teamResults[] = $homeTeamResult;
		} 
		$awayTeamResult = findTeamResult($fixture[$i]->awayTeamId, $teamResults);
		if ($awayTeamResult == null) {
			$awayTeamResult = new TeamResult(getTeamById($fixture[$i]->awayTeamId, false));
			$teamResults[] = $awayTeamResult;
		}
		$homeTeamScore = $fixture[$i]->getHomeTeamScore();
		$awayTeamScore = $fixture[$i]->getAwayTeamScore();
		if ($homeTeamScore == $awayTeamScore) {
			$homeTeamResult->addDraw(); 
			$awayTeamResult->addDraw();
		}
		else if ($homeTeamScore > $awayTeamScore) {
			$homeTeamResult->addWin();
			$homeTeamResult->doStreak(true);
			$awayTeamResult->doStreak(false);
		}
		else {
			$awayTeamResult->addWin();
			
			$homeTeamResult->doStreak(false);
			$awayTeamResult->doStreak(true);
		}
		
		$homeTeamResult->addFor($homeTeamScore);
		$homeTeamResult->addAgainst($awayTeamScore);
		$awayTeamResult->addFor($awayTeamScore);
		$awayTeamResult->addAgainst($homeTeamScore);
		
		if ($homeTeamScore > $homeTeamResult->highestScore) {
			$homeTeamResult->highestScore = $homeTeamScore;
		}
		if ($homeTeamScore < $homeTeamResult->lowestScore) {
			$homeTeamResult->lowestScore = $homeTeamScore;
		}
		if ($awayTeamScore > $awayTeamResult->highestScore) {
			$awayTeamResult->highestScore = $awayTeamScore;
		}
		if ($awayTeamScore < $awayTeamResult->lowestScore) {
			$awayTeamResult->lowestScore = $awayTeamScore;
		}
	}
	
	usort($teamResults, "compareTeamResult");
	
	return $teamResults;
}

function findTeamResult($teamId, $teamResults) {
	$count = count($teamResults);
	for ($i = 0; $i < $count; $i++) {
		if ($teamResults[$i]->team->id == $teamId) {
			return $teamResults[$i]; 
		}
	}
	
	return null;
}

function displayRoundForFixture($fixture) {
        $currentRoundStatus = getCurrentRoundStatus();
	$currentRound = getCurrentRound();
	$results = "";
	$count = count($fixture);
	$count2 = -1;
	$lastRound = -1;
	for ($i = 0; $i < $count; $i++) {
		$homeTeamScore = "";
		$awayTeamScore = "";
		if ($fixture[$i]->getHomeTeamScore() > 0) {
			$homeTeamScore = $fixture[$i]->getHomeTeamScore();
		}
		if ($fixture[$i]->getAwayTeamScore() > 0) {
			$awayTeamScore = $fixture[$i]->getAwayTeamScore();
		}
		$versesDescription = "verses";
		$isCurrent = false;
		if ($currentRound == $fixture[$i]->round) {
			$isCurrent = true;
		}
		if ($isCurrent && $currentRoundStatus != RoundStatusType::Open) {
			if ($homeTeamScore > $awayTeamScore) {
				$versesDescription = "defeating";
			}
			else if ($awayTeamScore > $homeTeamScore) {
				$versesDescription = "being defeated by";
			}
			else {
				$versesDescription = "deadlocked";
			}
		}
		else if ($currentRound > $fixture[$i]->round) {
			if ($homeTeamScore > $awayTeamScore) {
				$versesDescription = "defeated";
			}
			else if ($awayTeamScore > $homeTeamScore) {
				$versesDescription = "defeated by";
			}
			else {
				$versesDescription = "drew with";
			}			
		}
		if ($fixture[$i]->round != $lastRound) {
			$lastRound = $fixture[$i]->round;
			$results .= "<tr>\n";
			$results .= "<td><br></td>\n"; 
			$results .= "</tr>\n";
			$results .= "<tr>\n";
			$results .= "<td colspan=\"3\" align=\"center\"><h2>Round " . $fixture[$i]->round . "</td>\n"; 
			$results .= "</tr>\n";
		}
		$results .= "<tr>\n";
                $results .= "<td width=\"20%\">" . $fixture[$i]->homeTeamName . "</td><td width=\"10%\">" . $homeTeamScore;
		if ($isCurrent && $currentRoundStatus != RoundStatusType::Open) {
			$results .= " (" . $fixture[$i]->getHomePlayersPlayed() . " played)";
		}
		$results .= "</td><td width=\"20%\"><b>" . $versesDescription . "</b></td>\n";
		$results .= "<td width=\"20%\">" . $fixture[$i]->awayTeamName . "</td><td width=\"10%\">" . $awayTeamScore;
		if ($isCurrent && $currentRoundStatus != RoundStatusType::Open) {
			$results .= " (" . $fixture[$i]->getAwayPlayersPlayed() . " played)\n";
		}		
		$results .= "</td></tr>\n";
	}
	
	return $results;
}
?>